package com.ruoyi.custom.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.custom.domain.WorkAudit;
import com.ruoyi.custom.service.IWorkAuditService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作审计Controller
 * 
 * @author ruoyi
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/custom/audit")
public class WorkAuditController extends BaseController
{
    @Autowired
    private IWorkAuditService workAuditService;

    /**
     * 查询工作审计列表
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkAudit workAudit)
    {
        startPage();
        List<WorkAudit> list = workAuditService.selectWorkAuditList(workAudit);
        return getDataTable(list);
    }

    /**
     * 导出工作审计列表
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:export')")
    @Log(title = "工作审计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkAudit workAudit)
    {
        List<WorkAudit> list = workAuditService.selectWorkAuditList(workAudit);
        ExcelUtil<WorkAudit> util = new ExcelUtil<WorkAudit>(WorkAudit.class);
        util.exportExcel(response, list, "工作审计数据");
    }

    /**
     * 获取工作审计详细信息
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(workAuditService.selectWorkAuditById(id));
    }

    /**
     * 新增工作审计
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:add')")
    @Log(title = "工作审计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkAudit workAudit)
    {
        return toAjax(workAuditService.insertWorkAudit(workAudit));
    }

    /**
     * 修改工作审计
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:edit')")
    @Log(title = "工作审计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkAudit workAudit)
    {
        return toAjax(workAuditService.updateWorkAudit(workAudit));
    }

    /**
     * 删除工作审计
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:remove')")
    @Log(title = "工作审计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(workAuditService.deleteWorkAuditByIds(ids));
    }

    /**
     * 获取审计统计数据
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:stats')")
    @GetMapping("/stats")
    public AjaxResult getAuditStats(WorkAudit workAudit)
    {
        return success(workAuditService.getAuditStats(workAudit));
    }

    /**
     * 获取审计状态分布
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:status')")
    @GetMapping("/status/distribution")
    public AjaxResult getStatusDistribution(WorkAudit workAudit)
    {
        return success(workAuditService.getStatusDistribution(workAudit));
    }

    /**
     * 获取工作类型分布
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:type')")
    @GetMapping("/type/distribution")
    public AjaxResult getTypeDistribution(WorkAudit workAudit)
    {
        return success(workAuditService.getTypeDistribution(workAudit));
    }

    /**
     * 执行单个审计
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:execute')")
    @Log(title = "执行审计", businessType = BusinessType.UPDATE)
    @PostMapping("/execute")
    public AjaxResult executeAudit(@RequestBody WorkAudit workAudit)
    {
        return toAjax(workAuditService.executeAudit(workAudit));
    }

    /**
     * 批量审计
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:batch')")
    @Log(title = "批量审计", businessType = BusinessType.UPDATE)
    @PostMapping("/batch")
    public AjaxResult batchAudit(@RequestBody WorkAudit workAudit)
    {
        return toAjax(workAuditService.batchAudit(workAudit));
    }

    /**
     * 重新审计
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:reaudit')")
    @Log(title = "重新审计", businessType = BusinessType.UPDATE)
    @PostMapping("/reaudit")
    public AjaxResult reaudit(@RequestBody WorkAudit workAudit)
    {
        return toAjax(workAuditService.reaudit(workAudit));
    }

    /**
     * 获取审计历史
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:history')")
    @GetMapping("/history/{workId}")
    public AjaxResult getAuditHistory(@PathVariable("workId") String workId)
    {
        return success(workAuditService.getAuditHistory(workId));
    }

    /**
     * 获取审计设置
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:settings')")
    @GetMapping("/settings")
    public AjaxResult getAuditSettings()
    {
        return success(workAuditService.getAuditSettings());
    }

    /**
     * 更新审计设置
     */
    @PreAuthorize("@ss.hasPermi('custom:audit:settings:update')")
    @Log(title = "更新审计设置", businessType = BusinessType.UPDATE)
    @PutMapping("/settings")
    public AjaxResult updateAuditSettings(@RequestBody WorkAudit workAudit)
    {
        return toAjax(workAuditService.updateAuditSettings(workAudit));
    }
}
